import re

# 读取文件中的四字成语
with open('./asssets/chengyu.txt', 'r', encoding='utf-8') as f:
    chengyu_list = [line.strip() for line in f.readlines() if len(line.strip()) == 4]

# 匹配成语的不同格式
aabb_pattern = re.compile(r'(\w)\1(\w)\2')
aabc_pattern = re.compile(r'(\w)\1\w(\w)')
abac_pattern = re.compile(r'(\w)\w(\w)\1')

# 将成语按不同格式分类[存入]文件
for chengyu in chengyu_list:
    if aabb_pattern.match(chengyu):
        with open('./asssets/CY/CYAABB.txt', 'a', encoding='utf-8') as f1:
            f1.write(chengyu + '\n')
    elif aabc_pattern.match(chengyu):
        with open('./asssets/CY/CYAABC.txt', 'a', encoding='utf-8') as f2:
            f2.write(chengyu + '\n')
    elif abac_pattern.match(chengyu):
        with open('./asssets/CY/CYABAC.txt', 'a', encoding='utf-8') as f3:
            f3.write(chengyu + '\n')
